package it.xiuxian.personcenter.activity

import android.app.AlertDialog
import android.os.Build
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import it.xiuxian.lib.base.App
import it.xiuxian.lib.base.ArouterAddress
import it.xiuxian.lib.base.BaseActivity
import it.xiuxian.lib.utils.DensityUtil
import it.xiuxian.lib.utils.ImageUtil
import it.xiuxian.lib.utils.ToastUtil
import it.xiuxian.lib.utils.adapter.SlimAdapter
import it.xiuxian.lib.utils.adapter.SlimInjector
import it.xiuxian.lib.utils.adapter.injector.IViewInjector
import it.xiuxian.personcenter.R
import it.xiuxian.personcenter.bean.GongXianBean
import it.xiuxian.personcenter.bean.GongXianItemBean
import it.xiuxian.personcenter.bean.Item
import it.xiuxian.personcenter.databinding.PersonGongxianBinding
import it.xiuxian.personcenter.presenter.GongXianActivityPresenter


@Route(path = ArouterAddress.GONGXIANACTIVITY)
class GongXianActivity:BaseActivity<GongXianActivityPresenter>(){
   lateinit var adapter1: SlimAdapter
   lateinit var binding:PersonGongxianBinding
   var i=0
    override val layoutId: Int
        get() = R.layout.person_gongxian
    override val presenter: GongXianActivityPresenter
        get() = GongXianActivityPresenter()
    @RequiresApi(Build.VERSION_CODES.Q)
    override fun initView(){
        ARouter.getInstance().inject(this)
       binding = getViewDataBinding<PersonGongxianBinding>()
        binding.tvTijiao.setOnClickListener {

            var dialog = AlertDialog.Builder(this)
            var view=  View.inflate(this,R.layout.alert_dialog,null)
            var editText =  view.findViewById<EditText>(R.id.et_num)
            editText.setTextCursorDrawable(getDrawable(R.drawable.lib_cursor_color))
            dialog.setView(view).create()
           var show =  dialog.show()
          var d =  windowManager.defaultDisplay
            show.window?.setLayout((d.width*0.8).toInt(),LinearLayout.LayoutParams.WRAP_CONTENT)
            show.window?.setBackgroundDrawableResource(R.drawable.lib_shape_corner)
            view.findViewById<TextView>(R.id.tv_sure).setOnClickListener {
                if(editText.text.isNotEmpty()){
                    show.dismiss()
                    mPresenter!!.submit(editText.text.toString())
                }else{
                    ToastUtil.showShort("请输入数量")
                }

            }
            view.findViewById<TextView>(R.id.tv_cancel).setOnClickListener {
                show.dismiss()
            }

        }
        binding.rv.layoutManager = LinearLayoutManager(this)
        adapter1 = SlimAdapter.create().register<Item>(R.layout.person_gongxian_item,object :SlimInjector<Item>{
            override fun onInject(data: Item, injector: IViewInjector<out IViewInjector<*>>?) {

                injector!!.text(R.id.tv_name,data.nickname)
                       // .text(R.id.tv1,(++i).toString())
                    .text(R.id.tv_gongxianzhi,data.gongxianzhi.toString())

                ImageUtil.setCircleImage(data.faceurl, injector.findViewById<ImageView>(R.id.image) as ImageView)
                ++i
                if(i==1){
                    (injector.findViewById<ImageView>(R.id.image_one) as ImageView).visibility = View.VISIBLE
                    injector.visibility(R.id.tv1,View.GONE)
                    (injector.findViewById<ImageView>(R.id.image_one) as ImageView).setImageDrawable(resources.getDrawable(R.mipmap.person_one))
                }else if(i==2){
                    (injector.findViewById<ImageView>(R.id.image_one) as ImageView).visibility = View.VISIBLE
                    injector.visibility(R.id.tv1,View.GONE)
                    (injector.findViewById<ImageView>(R.id.image_one) as ImageView).setImageDrawable(resources.getDrawable(R.mipmap.person_two))
                }else if(i==3){
                    (injector.findViewById<ImageView>(R.id.image_one) as ImageView).visibility = View.VISIBLE
                    injector.visibility(R.id.tv1,View.GONE)
                    (injector.findViewById<ImageView>(R.id.image_one) as ImageView).setImageDrawable(resources.getDrawable(R.mipmap.person_three))
                }else{
                    (injector.findViewById<ImageView>(R.id.image_one) as ImageView).visibility = View.GONE
                    injector.visibility(R.id.tv1,View.VISIBLE).text(R.id.tv1,i.toString())
                }
                  // ImageUtil.setImage(data.faceurl,image)

            }

        }).attachTo(binding.rv)
    }

    override fun initData(){
       // super.initData()
        mPresenter!!.getGongXian()
    mPresenter!!.getList()
    }
    override fun initEvent(){
        binding.ivBack.setOnClickListener { finish() }
        //binding.cons.setPadding(0,DensityUtil.px2dip(App.INSTANCE!!.applicationContext),0,0)
        (binding.cons.layoutParams as LinearLayout.LayoutParams).setMargins(0,
            DensityUtil.getStatusPix(App.INSTANCE.applicationContext)+5,0,0)
    }
    lateinit var yuegongxianzhi:String
    fun setUI(data: GongXianBean){
        binding.tvZonggongxian.setText(data.yuegongxianzhi)
        binding.tvGongxian.setText(data.gongxianzhi)
       // binding.tv1.setText("点击一次贡献"+data.yuegongxianzhi+"个贡献值")
        yuegongxianzhi = data.yuegongxianzhi
        binding.tvShengyu.setText(data.yuemoney)
    }

    fun setList(data: GongXianItemBean){
        if(data.list.isNotEmpty()){
          //  LogUtils.e(data.list.toString(),Throwable())
            adapter1.updateData(data.list)
        }

    }

    fun setdata(msg: String){
        ToastUtil.showShort(msg)
        mPresenter!!.getGongXian()
        mPresenter!!.getList()
    }

    /* fun setGongXian(data: MonGongXianBean) {
     adapter1.updateData(data.list)
     }

     fun setZong(data: MonGongXianBean) {
         adapter1.updateData(data.list)
     }*/
}